package cn.mrlong.mdialogstyle.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import cn.mrlong.mdialogstyle.R;

public class AlertInputTextDialog {
    private Context context;
    private TextView title;
    private EditText msg;
    private TextView cancel;
    private ImageView line;
    private TextView sure;
    private Dialog dialog;
    private LinearLayout bg;
    private boolean isShowTitle = false;
    private boolean isShowCancelBtn = false;

    public AlertInputTextDialog(Context context) {
        this.context = context;
    }

    public AlertInputTextDialog builder() {

        //加载布局
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_input, null);
        bg = view.findViewById(R.id.ll_bg);
        title = view.findViewById(R.id.tv_title);
        msg = view.findViewById(R.id.ed_content);
        cancel = view.findViewById(R.id.btn_cancel);
        line = view.findViewById(R.id.img_line);
        sure = view.findViewById(R.id.btn_sure);
        //控制显示
        title.setVisibility(View.GONE);
        cancel.setVisibility(View.GONE);
        line.setVisibility(View.GONE);

        //定义dialog
        dialog = new Dialog(context, R.style.AlertDialogStyle);
        dialog.setContentView(view);

        //控制大小
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        bg.setLayoutParams(new LinearLayout.LayoutParams((int) (display.getWidth() * 0.8), LinearLayout.LayoutParams.WRAP_CONTENT));
        return this;
    }

    public void show() {
        setLayoutShow();
        dialog.show();
    }

    /**
     * 控制布局
     */
    private void setLayoutShow() {
        if (isShowCancelBtn) {
            cancel.setVisibility(View.VISIBLE);
            line.setVisibility(View.VISIBLE);
        } else {
            cancel.setVisibility(View.GONE);
            line.setVisibility(View.GONE);
        }
        if (isShowTitle) {
            title.setVisibility(View.VISIBLE);
        } else {
            title.setVisibility(View.GONE);
        }
    }

    /**
     * 取消按钮触发事件和显示
     *
     * @param cancelStr
     * @param listener
     * @return
     */
    public AlertInputTextDialog setCancelBtn(String cancelStr, final View.OnClickListener listener) {
        isShowCancelBtn = true;
        if (null != cancelStr && !"".equals(cancelStr)) {
            cancel.setText(cancelStr);
        } else {
            cancel.setText("取消");
        }
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onClick(view);
                dialog.dismiss();
            }
        });

        return this;
    }

    /**
     * 设置点击启动按钮文字和点击事件
     *
     * @param postStr
     * @param listener
     * @return
     */
    public AlertInputTextDialog setPostBtn(String postStr, final PostClickListener listener) {

        if (null != postStr && !"".equals(postStr)) {
            sure.setText(postStr);
        } else {
            sure.setText("确定");
        }
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String content = msg.getText().toString().trim();
                if (null != content && !"".equals(content))
                    listener.onClick(content);
                else
                    Toast.makeText(context, "请输入", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });

        return this;
    }

    public interface PostClickListener {
        void onClick(String content);
    }

    /**
     * 点击空白区域是否消失
     *
     * @param cancel
     * @return
     */
    public AlertInputTextDialog setCanceledOnTouchOutside(boolean cancel) {
        dialog.setCanceledOnTouchOutside(cancel);
        return this;
    }


    /**
     * 点击返回键是否消失
     *
     * @param cancel
     * @return
     */
    public AlertInputTextDialog setCancelable(boolean cancel) {
        dialog.setCancelable(cancel);
        return this;
    }

    /**
     * 设置提示内容
     *
     * @param msgStr
     * @return
     */
    public AlertInputTextDialog setMsgHint(String msgStr) {
        if (null != msgStr && !"".equals(msgStr)) {
            msg.setHint(msgStr);
        } else {
            msg.setHint("请输入");
        }
        return this;
    }

    /**
     * 设置提示标题
     *
     * @param titleStr
     * @return
     */
    public AlertInputTextDialog setTitle(String titleStr) {
        isShowTitle = true;
        if (null != titleStr && !"".equals(titleStr)) {
            title.setText(titleStr);
        } else {
            title.setText("标题");
        }
        return this;
    }


}
